clear all
set more off
capture log close
log using "$output\table9.log", replace

use "$data/cpsmergeddata.dta", clear

*** total adults
gen pop=(asecwt + spouseasecwt)
egen totadults=sum(pop)
drop pop

egen totkids=sum(newnumkidswt17)
egen totpoorkids=sum(newnumkidswt17*poor)
sum totpoorkids
drop totpoorkids 

** total effect on poverty
gen pooradults=(age>=18)*poor*(asecwt+spouseasecwt)
egen totpooradults=sum(pooradults)

gen nolongerpooradults=(age>=18)*poor*(poorctc21==0)*(asecwt+spouseasecwt)
egen totnolongerpooradults=sum(nolongerpooradults)

gen stillpooradults=(age>=18)*poor*(poorctc21==1)*(asecwt+spouseasecwt)
egen totstillpooradults=sum(stillpooradults)


* row 1
gen double group=(married==0)*(incwage>0)*(v25>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=asecwt*avgtax*.75*group
egen totgroup1a=sum(group1)

gen double group2=asecwt*avgtax*.75*group*poor
egen totgroup2a=sum(group2)

gen double group3=asecwt*avgtax*.75*group*(poor==0)
egen totgroup3a=sum(group3)

gen double group4=asecwt*avgtax*.75*group*(poor==0)*poorctc21nother
egen totgroup4a=sum(group4)

gen double group5=asecwt*avgtax*.75*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5a=sum(group5)

gen pov1=(poor*group)/(group)
gen pov2=(poor*group*(poorctc21==1))/(group)
gen pov3=(poor*group*(poorctc21==1)+group4/asecwt+group5/asecwt)/(group)

egen pop=sum(group*asecwt)
forvalues i=1/3{
	egen povnum`i'a=sum(pov`i'*asecwt)
	gen povrate`i'=povnum`i'a/pop
	}
	
gen totgroup45a=totgroup4a+totgroup5a	
sum pop totgroup1a totgroup2a totgroup45a povnum1a povnum2a povnum3a povrate1 povrate2 povrate3
sum avgtax if group==1 [aw=asecwt]

di "pov % decrease="
di (povnum1a-povnum3a)/povnum1a
drop pov1 pov2 pov3 povrate* pop group5 group*

* row 2
gen double group=(married==0)*(incwage>0)*(v25==0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=asecwt*avgtax*.25*group
egen totgroup1b=sum(group1)

gen double group2=asecwt*avgtax*.25*group*poor
egen totgroup2b=sum(group2)

gen double group3=asecwt*avgtax*.25*group*(poor==0)
egen totgroup3b=sum(group3)

gen double group4=asecwt*avgtax*.25*group*(poor==0)*poorctc21nother
egen totgroup4b=sum(group4)

gen double group5=asecwt*avgtax*.25*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5b=sum(group5)

gen pov1=(poor*group)/(group)
gen pov2=(poor*group*(poorctc21==1))/(group)
gen pov3=(poor*group*(poorctc21==1)+group4/asecwt+group5/asecwt)/(group)

egen pop=sum(group*asecwt)
forvalues i=1/3{
	egen povnum`i'b=sum(pov`i'*asecwt)
	gen povrate`i'=povnum`i'b/pop
	}
	
gen totgroup45b=totgroup4b+totgroup5b	
sum pop totgroup1b totgroup2b totgroup45b povnum1b povnum2b povnum3b povrate1 povrate2 povrate3
sum avgtax if group==1 [aw=asecwt]

di "pov % decrease="
di (povnum1b-povnum3b)/povnum1b
drop pov1 pov2 pov3 povrate* pop group5 group*

* row 3
gen double group=(married==1)*(incwage>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=asecwt*avgtax*.25*group
egen totgroup1c=sum(group1)

gen double group2=asecwt*avgtax*.25*group*poor
egen totgroup2c=sum(group2)

gen double group3=asecwt*avgtax*.25*group*(poor==0)
egen totgroup3c=sum(group3)

gen double group4=asecwt*avgtax*.25*group*(poor==0)*poorctc21nother
egen totgroup4c=sum(group4)

gen double group5=asecwt*avgtax*.25*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5c=sum(group5)

gen pov1=(poor*group)/(group)
gen pov2=(poor*group*(poorctc21==1))/(group)
gen pov3=(poor*group*(poorctc21==1)+group4/asecwt+group5/asecwt)/(group)

egen pop=sum(group*asecwt)
forvalues i=1/3{
	egen povnum`i'c=sum(pov`i'*asecwt)
	gen povrate`i'=povnum`i'c/pop
	}

gen totgroup45c=totgroup4c+totgroup5c	
sum pop totgroup1c totgroup2c totgroup45c povnum1c povnum2c povnum3c povrate1 povrate2 povrate3
sum avgtax if group==1 [aw=asecwt]

di "pov % decrease="
di (povnum1c-povnum3c)/povnum1c
drop pov1 pov2 pov3 povrate* pop group5 group*

* row 4
gen double group=(married==1)*(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=asecwt*newrtwchangespouse*.25*group
egen totgroup1d=sum(group1)

gen double group2=asecwt*newrtwchangespouse*.25*group*poor
egen totgroup2d=sum(group2)

gen double group3=asecwt*newrtwchangespouse*.25*group*(poor==0)
egen totgroup3d=sum(group3)

gen double group4=asecwt*newrtwchangespouse*.25*group*(poor==0)*poorctc21notspouse
egen totgroup4d=sum(group4)

gen double group5=asecwt*newrtwchangespouse*.25*group*(poor==1)*(poorctc21==0)*poorctc21notspouse
egen totgroup5d=sum(group5)

gen pov1=(poor*group)/(group)
gen pov2=(poor*group*(poorctc21==1))/(group)
gen pov3=(poor*group*(poorctc21==1)+group4/asecwt+group5/asecwt)/(group)

egen pop=sum(group*asecwt)
forvalues i=1/3{
	egen povnum`i'd=sum(pov`i'*asecwt)
	gen povrate`i'=povnum`i'd/pop
	}
	
gen totgroup45d=totgroup4d+totgroup5d	
sum pop totgroup1d totgroup2d totgroup45d povnum1d povnum2d povnum3d povrate1 povrate2 povrate3
sum newrtwchangespouse if group==1 [aw=asecwt]

di "pov % decrease="
di (povnum1d-povnum3d)/povnum1d
drop pov1 pov2 pov3 povrate* pop group5 group*


* row 5: sum up non-mom and potential spouse
gen person1=(incwage>0)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)
gen person2=(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(married==1)

gen double group1=(asecwt*person1+spouseasecwt*person2)*avgtax*.25
egen totgroup1e=sum(group1)

gen double group2=(asecwt*person1+spouseasecwt*person2)*avgtax*.25*poor
egen totgroup2e=sum(group2)

gen double group3=(asecwt*person1+spouseasecwt*person2)*avgtax*.25*(poor==0)
egen totgroup3e=sum(group3)

gen double group4=(asecwt*person1)*avgtax*.25*(poor==0)*poorctc21nother+(spouseasecwt*person2)*avgtax*.25*(poor==0)*poorctc21notspouse
egen totgroup4e=sum(group4)

gen double group5=(asecwt*person1)*avgtax*.25*(poor==1)*(poorctc21==0)*poorctc21nother+spouseasecwt*person2*avgtax*.25*(poor==1)*(poorctc21==0)*poorctc21notspouse
egen totgroup5e=sum(group5)

gen pov1=(poor*(person1*asecwt+person2*spouseasecwt))
gen pov2=(poorctc21*(person1*asecwt+person2*spouseasecwt))
gen pov3=(poorctc21*(person1*asecwt+person2*spouseasecwt)+group4+group5)

egen pop=sum(asecwt*person1+spouseasecwt*person2)
forvalues i=1/3{
	egen povnum`i'e=sum(pov`i')
	gen povrate`i'=povnum`i'e/pop
	}

gen totgroup45e=totgroup4e+totgroup5e	
sum pop totgroup1e totgroup2e totgroup45e povnum1e povnum2e povnum3e povrate1 povrate2 povrate3
sum newrtwchangespouse if person1 | person2 [aw=asecwt]

di "pov % decrease="
di (povnum1e-povnum3e)/povnum1e
drop pov1 pov2 pov3 povrate* group5 group* pop

gen pop=(asecwt*(incwage>0) + spouseasecwt*(totincwage>incwage))*(agi2017<80000)
egen totpop=sum(pop)

forvalues i=1/2{
	di totgroup`i'a+totgroup`i'b+totgroup`i'c+totgroup`i'd+totgroup`i'e
}
di totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e

di totpooradults
di totstillpooradults
di (totstillpooradults+totgroup4a+totgroup4b+2*totgroup4c+2*totgroup4d+totgroup4e+totgroup5a+totgroup5b+2*totgroup5c+2*totgroup5d+totgroup5e)
di totpooradults/totadults
di totstillpooradults/totadults
di (totstillpooradults+totgroup4a+totgroup4b+2*totgroup4c+2*totgroup4d+totgroup4e+totgroup5a+totgroup5b+2*totgroup5c+2*totgroup5d+totgroup5e)/totadults
di "pov % decrease="
di (totpooradults-(totstillpooradults+totgroup4a+totgroup4b+2*totgroup4c+2*totgroup4d+totgroup4e+totgroup5a+totgroup5b+2*totgroup5c+2*totgroup5d+totgroup5e))/totpooradults
drop totgroup* povnum* person* pop totpop


* KIDS 

drop stillpoor totstillpoor 

gen poorkids=poor*(newnumkidswt17)
egen totpoorkids=sum(poorkids)
di totpoorkids

gen stillpoor=poor*(poorctc21==1)*(newnumkidswt17)
egen totstillpoor=sum(stillpoor)

di totkids
di totpoorkids
di totstillpoor

di totpoorkids/totkids
di (totstillpoor)/totkids

* row 1
gen double group=(married==0)*(incwage>0)*(v25>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*.75*group
egen totgroup1a=sum(group1)
gen double group2=newnumkidswt17*avgtax*.75*group*poor
egen totgroup2a=sum(group2)

gen double group3=newnumkidswt17*avgtax*.75*group*(poor==0)
egen totgroup3a=sum(group3)

gen double group4=newnumkidswt17*avgtax*.75*group*(poor==0)*poorctc21nother
egen totgroup4a=sum(group4)

gen double group5=newnumkidswt17*avgtax*.75*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5a=sum(group5)

gen pov1=(poor*group)/(group)
gen pov2=(poor*group*(poorctc21==1))/(group)
gen pov3=(poor*group*(poorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'a=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'a/pop
	}
drop pov1 pov2 pov3 povrate* pop group5 group*

* row 2
gen double group=(married==0)*(incwage>0)*(v25==0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*.25*group
egen totgroup1b=sum(group1)

gen double group2=newnumkidswt17*avgtax*.25*group*poor
egen totgroup2b=sum(group2)

gen double group3=newnumkidswt17*avgtax*.25*group*(poor==0)
egen totgroup3b=sum(group3)

gen double group4=newnumkidswt17*avgtax*.25*group*(poor==0)*poorctc21nother
egen totgroup4b=sum(group4)

gen double group5=newnumkidswt17*avgtax*.25*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5b=sum(group5)

gen pov1=(poor*group)/(group)
gen pov2=(poor*group*(poorctc21==1))/(group)
gen pov3=(poor*group*(poorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'b=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'b/pop
	}
drop pov1 pov2 pov3 povrate* pop group5 group*

* row 3
gen double group=(married==1)*(incwage>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*.25*group
egen totgroup1c=sum(group1)

gen double group2=newnumkidswt17*avgtax*.25*group*poor
egen totgroup2c=sum(group2)

gen double group3=newnumkidswt17*avgtax*.25*group*(poor==0)
egen totgroup3c=sum(group3)

gen double group4=newnumkidswt17*avgtax*.25*group*(poor==0)*poorctc21nother
egen totgroup4c=sum(group4)

gen double group5=newnumkidswt17*avgtax*.25*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5c=sum(group5)

gen pov1=(poor*group)/(group)
gen pov2=(poor*group*(poorctc21==1))/(group)
gen pov3=(poor*group*(poorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'c=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'c/pop
	}
drop pov1 pov2 pov3 povrate* pop group5 group*

* row 4
gen double group=(married==1)*(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*newrtwchangespouse*.25*group
egen totgroup1d=sum(group1)

gen double group2=newnumkidswt17*newrtwchangespouse*.25*group*poor
egen totgroup2d=sum(group2)

gen double group3=newnumkidswt17*newrtwchangespouse*.25*group*(poor==0)
egen totgroup3d=sum(group3)

gen double group4=newnumkidswt17*newrtwchangespouse*.25*group*(poor==0)*poorctc21notspouse
egen totgroup4d=sum(group4)

gen double group5=newnumkidswt17*newrtwchangespouse*.25*group*(poor==1)*(poorctc21==0)*poorctc21notspouse
egen totgroup5d=sum(group5)

gen pov1=(poor*group)/(group)
gen pov2=(poor*group*(poorctc21==1))/(group)
gen pov3=(poor*group*(poorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'd=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'd/pop
	}
drop pov1 pov2 pov3 povrate* pop group5 group*

* row 5: sum up non-mom and potential spouse
gen person1=(incwage>0)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)
gen person2=(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(married==1)

gen double group1=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.25
egen totgroup1e=sum(group1)

gen double group2=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.25*poor
egen totgroup2e=sum(group2)

gen double group3=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.25*(poor==0)
egen totgroup3e=sum(group3)

gen double group4=(newnumkidswt17*person1)*avgtax*.25*(poor==0)*poorctc21nother+(newnumkidswt17*person2)*avgtax*.25*(poor==0)*poorctc21notspouse
egen totgroup4e=sum(group4)

gen double group5=(newnumkidswt17*person1)*avgtax*.25*(poor==1)*(poorctc21==0)*poorctc21nother+newnumkidswt17*person2*avgtax*.25*(poor==1)*(poorctc21==0)*poorctc21notspouse
egen totgroup5e=sum(group5)

gen pov1=(poor*(person1*newnumkidswt17+person2*newnumkidswt17))
gen pov2=(poorctc21*(person1*newnumkidswt17+person2*newnumkidswt17))
gen pov3=(poorctc21*(person1*newnumkidswt17+person2*newnumkidswt17)+group4+group5)

egen pop=sum(newnumkidswt17*person1+newnumkidswt17*person2)
forvalues i=1/3{
	egen povnum`i'e=sum(pov`i')
	gen povrate`i'=povnum`i'e/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

forvalues i=1/2{
	di totgroup`i'a+totgroup`i'b+totgroup`i'c+totgroup`i'd+totgroup`i'e
}
di totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e

di totkids
di totpoorkids
di totstillpoor
di (totstillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)
di totpoorkids/totkids
di totstillpoor/totkids
di (totstillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)/totkids
di "pov % decrease="
di (totpoorkids-(totstillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e))/totpoorkids
drop totgroup* povnum* person*

log close